import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { MyModalHelper } from '../../../core/my-modal.helper';
import { NzMessageService } from 'ng-zorro-antd';
import { CompanyService } from '../../../core/routes-services/company.service';
import { ActivityProductService } from '../../../core/routes-services/activity-product.service';
import { myValidNull } from '@core/util/string-utils';

@Component({
  selector: 'app-company-list',
  templateUrl: './company-list.component.html',
})
export class CompanyListComponent implements OnInit {
  isLoading = false;
  keyword: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };


  allProducts:any [] = [];
  constructor(private message: NzMessageService,
              private router: Router,
              private companyService:CompanyService,
              private activityProductService:ActivityProductService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initOption()
    this.initPage();
  }

  initOption(){
    this.isLoading = true;
    this.activityProductService.getAll().then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.allProducts = res.data.activityProductsList;
        console.log(this.allProducts);
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  initPage() {
    this.isLoading = true;
    this.companyService.pagination(this.page, this.keyword).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  addEntity = () => {
    this.router.navigate(['/company/list/add'], { queryParams: { id: 0 } });
  };


  setInActivity = (data) => {
    this.companyService.setInActivity(data.id,{}).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.message.success("设置成功");
        this.changePageNo(this.page.number);
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }
  setUnActivity = (data) => {
    this.companyService.setUnActivity(data.id,{}).then((res) => {
      if (res.status.toString().startsWith('20')) {
        if(myValidNull(data.topNum)){
          this.message.success("设置成功");
        }else {
          this.message.success("下线成功，下线活动有影响小程序首页排版，请注意修改");
        }
        this.changePageNo(this.page.number);
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }
}
